using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DevExpress.Mvvm;
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.Data;
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.Docking.VisualElements;
// using DevExpress.Xpf.Docking;
// using DevExpress.Xpf.Docking.VisualElements;
using RFS.BizLogic.PaiShi;
using RFS.BizLogic.Sys;
using RFS.Domain.PaiShi;
using RFS.Domain.Sys;
using RFS.WPF.StartUp;
using RFS.WPF.utils;
using RFS.WPF.Views.DuiZhang;
using RFS.WPF.Views.Settings;
using RFS.WPF.Views.Settings.HolidayPlan;
using BindableBase = Prism.Mvvm.BindableBase;
using DelegateCommand = Prism.Commands.DelegateCommand;
using MenuItem = RFS.Domain.Sys.MenuItem;

namespace RFS.WPF.Views
{

    public class DocumentViewModel : BindableBase
    {
        public string Title { get; set; }
        public VoucherSetWrapperVm Vm { get; set; }

        public DocumentViewModel(string title, VoucherSetWrapperVm content)
        {
            Title = title;
            Vm = content;
        }

    }
    

    public class SysVm : BindableBase
    {
        private int ii = 0;

        void openDoc(BizFlowNodeEnum leftNode= BizFlowNodeEnum.FaHuo)
        {

            // Create a new Document
            // DocumentGroup documentGroup = new DocumentGroup();

            // Create a new Document
            // Document document = new Document();

            // Create a UserControl (replace with your actual UserControl)

            var vm=_en.Get<VoucherSetWrapperVm>();
    
            if (leftNode== BizFlowNodeEnum.Zuhuo)
                vm.SwichLeftNodeFromFahuoToZuhuoNode();
            else
            {
                vm.SwichLeftNodeFromZuhuoNode2Fahuo();
            }
            
            // Create a new Document
            DocumentGroup documentGroup = _documentGroup ;

            // Create a new Document
            var  document = new DocumentPanel();

            document.Caption = $"{vm} {++ii}";

            // Create a UserControl (replace with your actual UserControl)

            var uc = new VoucherSetView(vm);
            

            // RaisePropertyChanged(nameof(Docs));
            // Set the Content of the Document to the UserControl
            document.Content = uc; 

            // Add the Document to the DocumentGroup
            documentGroup.Items.Add(document);

            // Assign the DocumentGroup to the DocumentPanel
            // documentPanel.Documents.Add(documentGroup); 

        }
        
        #region Docs

        private DocumentGroup _documentGroup;

        public void RegisterDocument(DocumentGroup dg)
        {
            _documentGroup = dg;
        }
        
        private ObservableCollection<DocumentViewModel> _docs=new ObservableCollection<DocumentViewModel>() ;

        public ObservableCollection<DocumentViewModel> Docs
        {
            get { return _docs; }
            set
            {
                _docs = value;
                RaisePropertyChanged(nameof(Docs));
            }
        }

        #endregion
        // 示例数据填充

        public MenuMain CreateMenu()
        {
            var menu = new MenuMain { Items = new List<MenuItem>() };
            
            
            // 甲-字典准备
            var dictPs = new MenuItem("拍市账务稽核")
            {
                SubItems = new List<string>
                {
                   MenuMain.拍市录入数对验收,
                   MenuMain.对账_组货vs验收 ,
                }
            };
            menu.Items.Add(dictPs);

            
            
             

            //
            //
            // // 甲-字典准备
            // var dictPrep = new MenuItem("甲-字典准备")
            // {
            //     SubItems = new List<string>
            //     {
            //         "上传文件",
            //         "1 基准产量 Q0",
            //         "2 基准价格 P0",
            //         "3 基准长度比例 L0"
            //     }
            // };
            // menu.Items.Add(dictPrep);
            //
            //
            //
            //
            // // 乙-基础信息
            // var basicInfo = new MenuItem("乙-基础信息")
            // {
            //     SubItems = new List<string>
            //     {
            //         "1 定值信息 D0",
            //         "2 主管种植计划 J1",
            //         "3 标准种植计划 J0",
            //         "4 标准收入计划 W0",
            //         "5 岗位配置计划 Z0",
            //         "6 薪酬发放计划 X0"
            //     }
            // };
            // menu.Items.Add(basicInfo);
            //
            // // 丙-财务凭证
            // var financialVoucher = new MenuItem("丙-财务凭证")
            // {
            //     SubItems = new List<string>
            //     {
            //         "1 当期产量",
            //         "2 参照拍价",
            //         "3 当量收入",
            //         "4 波动系数",
            //         "5 内供应收"
            //     }
            // };
            // menu.Items.Add(financialVoucher);
            //
            // // 丁-车间凭证
            // var workshopVoucher = new MenuItem("丁-车间凭证")
            // {
            //     SubItems = new List<string>
            //     {
            //         "1 昨日拍价",
            //         "2 执行产量_种",
            //         "3 执行产量_包",
            //         "4 结算收入"
            //     }
            // };
            // menu.Items.Add(workshopVoucher);
            //
            // // 戊-完成比例
            // var completionRatio = new MenuItem("戊-完成比例")
            // {
            //     SubItems = new List<string>
            //     {
            //         "1 地块完成比例",
            //         "2 药块完成比例",
            //         "3 分组完成比例",
            //         "4 车间完成比例",
            //         "5 车间按岗薪酬"
            //     }
            // };
            // menu.Items.Add(completionRatio);
            //
            // // 己-薪酬核算
            // var salaryCalculation = new MenuItem("己-薪酬核算")
            // {
            //     SubItems = new List<string>
            //     {
            //         "1 [种]田岗核算薪酬",
            //         "2 [种]药岗核算薪酬",
            //         "3 [种]领班岗核算薪酬",
            //         "4 [种]主管岗核算薪酬",
            //         "5 [种]拉花岗核算薪酬",
            //         "6 [种]机动组总薪酬",
            //         "7 [种]车间其他薪酬",
            //         "1 [选]选包岗核算薪酬",
            //         "2 [选]复检岗核算薪酬",
            //         "3 [选]余库岗核算薪酬",
            //         "4 [选]培训组核算薪酬"
            //     }
            // };
            // menu.Items.Add(salaryCalculation);
            //
            // // 庚-薪酬发放
            // var salaryRelease = new MenuItem("庚-薪酬发放")
            // {
            //     SubItems = new List<string>()
            // };
            // menu.Items.Add(salaryRelease);

            return menu;
        }

        IConfigT _config;
        public SysVm(IConfigT config, IPaiShiOrg ps, IAntiObject en)
        {
            _config = config;
            _ps = ps;
            _en = en;

            // openDoc();

            CmdNevBarItemExcute = new Prism.Commands.DelegateCommand<string>(DoCmdNevBarItemExcute);
            CmdHolidayPlanSetting = new DelegateCommand (DoCmdHolidayPlanSetting);
            
            var menu = _config.ConfigSys.MainMenu;
            menu = null;
            if (menu == null)
            {
                menu = CreateMenu();
                _config.ConfigSys.MainMenu =  menu ;
                SaveConfig();
            }

            MenutItems = menu.Items ;

            
            _cmds= GetAllCommands(this)
                .Where(x=>x is DelegateCommand)
                .Select(x=>x as DelegateCommand);


            Init();
        }

        private void DoCmdHolidayPlanSetting()
        {

            var v = new HolidayPlansView(this);
            v.ShowDialog();
            
        }

        private void Init()
        {

            Task.Run(() =>
            {

                LoadPs();
            });
        }

        private IAntiObject _en;
        
        private void DoCmdNevBarItemExcute(string s)
        {

            if (s.Contains(MenuMain.上传文件))
            {
                DoOpenFilesUploaded();
            }           
            
            if (s.Contains(MenuMain.拍市录入数对验收))
            {
                if (_config.ConfigSys.CompactFormStyle)
                {
                    openDoc();
                }
                else
                {
                    var v = _en.Get<VoucherSetWindow>();
                    v.WindowState = WindowState.Maximized;
                    v.Show ();
                }
                // openDoc();
               
            }
            
                      
            if (s.Contains(MenuMain.对账_组货vs验收))
            {
                if (_config.ConfigSys.CompactFormStyle)
                {
                    openDoc(BizFlowNodeEnum.Zuhuo );
                }
                else
                {
                    var v = _en.Get<VoucherSetWindow>();
                    v.WindowState = WindowState.Maximized;
                    v.Show ();
                }
                // openDoc();
               
            }
            
            
            
            
        }

        
        private void DoOpenFilesUploaded()
        {
            Console.WriteLine("open files uploaded");
            
            
        }

        public   List<ICommand> GetAllCommands(object viewModel)
        {
            var commands = new List<ICommand>();

            // Get all properties of the view model
            PropertyInfo[] properties = viewModel.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            // Iterate through properties
            foreach (PropertyInfo property in properties)
            {
                // Check if the property type is ICommand
                if (typeof(ICommand).IsAssignableFrom(property.PropertyType))
                {
                    // Get the value of the property (the command)
                    ICommand command = (ICommand)property.GetValue(viewModel);

                    // Add the command to the list
                    commands.Add(command);
                }
            }

            return commands;
        }



        private void SaveConfig()
        {
            //save config
            
        }


        public  List<MenuItem> MenutItems { get; set; }
        // public  CmdNevBarItemExcute { get; set; } 
        public Prism.Commands.DelegateCommand<string> CmdNevBarItemExcute { get; set; }
        public DelegateCommand CmdHolidayPlanSetting { get; set; }

        IEnumerable <DelegateCommand>  _cmds  =new List<DelegateCommand>();

        void NoteCommandsState()
        {
            
            foreach (var d in _cmds)
            {
                d.RaiseCanExecuteChanged();
            }
        }


        private IPaiShiOrg _ps;
        public void LoadPs()
        {
            //load from 
            var rps=_ps.GetHolidayPlans().ToList();


            var ls = new List<VmHolidayPlanItem>();
            
            for (int i = 0; i < 20; i++)
            {
                var h=new VmHolidayPlanItem();
                if (i < rps.Count )
                {
                    h.StartAt=rps[i].StartAt;   
                    h.EndAt=rps[i].EndAt;   
                    h.Remark=rps[i].Remark;   
                }
                
                ls.Add(h);
            }


            HolidayPlans = new ObservableCollection<VmHolidayPlanItem>(ls);
            
            1.UseUIThread(()=> RaisePropertyChanged("HolidaysPlans") ) ;


        }


        #region IsCompactFormsStyle

        // private bool _IsCompactFormsStyle;

        public bool IsCompactFormsStyle
        {
            get { return _config.ConfigSys.CompactFormStyle; }
            set
            {
                _config.ConfigSys.CompactFormStyle = value;
                RaisePropertyChanged(nameof(IsCompactFormsStyle));
            }
        }

        #endregion
        
        


        public ObservableCollection<VmHolidayPlanItem> HolidayPlans { get; set; } =
            new ObservableCollection<VmHolidayPlanItem>();

    }
    
    
}